#include "hrv.h"
#include "ui_hrv.h"
#include"QPainter"
#include "qsettings.h"
#include "HRVPro/hrvdataprocess.h"
#include "Config/constant.h"

HRV::HRV(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::HRV)
{
    ui->setupUi(this);
    Series_HRV = new QLineSeries;
    xAxis_HRV = new QValueAxis();
    yAxis_HRV = new QValueAxis();
    Series_AR = new QLineSeries;
    xAxis_AR = new QValueAxis();
    yAxis_AR = new QValueAxis();
    InitialAxis_Chart_HRV();
    InitialAxis_Chart_AR();
    chart=CreateLineChart_HRV();
    chart_AR=CreateLineChart_AR();

    QSettings setting("./Configuration.ini",QSettings::IniFormat);
    setting.beginGroup("Config");
    QString reportName=setting.value("ReportName").toString();
    setting.endGroup();
    ui->label_SL_Title->setText(reportName+"（HRV）分析报告");
}

HRV::~HRV()
{
    delete Series_HRV;
    delete xAxis_HRV;
    delete yAxis_HRV;
    delete chart;
    delete Series_AR;
    delete xAxis_AR;
    delete yAxis_AR;
    delete chart_AR;
    delete ui;
}
void HRV::setValue(DataBeans dataBean)
{
    QSettings *setting=new QSettings("./Configuration.ini",QSettings::IniFormat);
    setting->beginGroup("Config");
    QString JGName=setting->value("User").toString();
    setting->endGroup();
    delete setting;
    HRVDataProcess hrvdataPro;
    HRVIndex hi=hrvdataPro.Json2HRVData(dataBean.hrvIndexResults);
    ui->label_DW->setText(JGName);
    ui->label_display_ID->setText(dataBean.UserID);
    ui->label_display_name->setText(dataBean.UserName);
    ui->label_display_age->setText(dataBean.UserAge+"/"+dataBean.UserSex);
    ui->label_display_time->setText(dataBean.MeasureTime);
    ui->label_3_dis_RRM->setText(QString::number(hi.time.RR_mean));
    ui->label_3_dis_SDNN->setText(QString::number(hi.time.RR_SDNN,'f',1));
    ui->label_3_dis_RMSSD->setText(QString::number(hi.time.RR_RMSSD,'f',1));
    ui->label_3_dis_DRR->setText(QString::number(hi.time.RR_DRR));
    ui->label_3_dis_APEN->setText(QString::number(hi.time.ApEn,'f',2));
    ui->label_3_dis_NN50->setText(QString::number(hi.time.NN50,'f',2));

    ui->label_3_dis_TP->setText(QString::number(hi.freq.TP,'f',1));
    ui->label_3_dis_VLF->setText(QString::number(hi.freq.VLF,'f',1));
    ui->label_3_dis_LF->setText(QString::number(hi.freq.LF,'f',1));
    ui->label_3_dis_HF->setText(QString::number(hi.freq.HF,'f',1));
    ui->label_3_dis_LFHF->setText(QString::number(hi.freq.LFHF,'f',2));



    ui->widget_RR_wave->setChart(chart);
    ui->widget_RR_wave->setRenderHint(QPainter::Antialiasing,true);
    setHRVData(hi.time.RR);

    ui->widget_AR->setChart(chart_AR);
    ui->widget_AR->setRenderHint(QPainter::Antialiasing,true);
    setARData(hi.freq.freq,hi.freq.psd);
    ui->textEdit_HRV_Res->setText(hi.HRVResults);

}


void HRV::InitialAxis_Chart_HRV()
{
    yAxis_HRV->setRange(300, 1500);
    xAxis_HRV->setRange(0, 360);
    xAxis_HRV->setLabelsFont( QFont("微软雅黑", 12, QFont::Normal, false));
    yAxis_HRV->setLabelsFont( QFont("微软雅黑", 12, QFont::Normal, false));
    yAxis_HRV->setTitleText("心率曲线图");
    yAxis_HRV->setTitleBrush(QBrush(QColor(0x2E,0x5A,0x82),Qt::SolidPattern));
    yAxis_HRV->setLabelsColor(QColor(0x2E,0x5A,0x82));
    yAxis_HRV->setGridLineVisible(false);
    yAxis_HRV->setGridLineVisible(true);
    yAxis_HRV->setGridLineColor(QColor(0xC4,0x91,0x44,0x20));
    xAxis_HRV->setLineVisible(false);
    xAxis_HRV->setVisible(false);
    yAxis_HRV->setTitleText("心率(Beat/Min)");
    yAxis_HRV->setVisible(true);
}
QChart* HRV::CreateLineChart_HRV()
{
    QChart *chart = new QChart();
    Series_HRV->setPen(QPen(QColor(0x00,0x00,0x00),2));//设置波形颜色
    chart->addSeries(Series_HRV);
    chart->setBackgroundVisible(true);
    chart->legend()->hide(); /* hide legend */
    chart->addAxis(xAxis_HRV, Qt::AlignBottom);
    chart->addAxis(yAxis_HRV, Qt::AlignLeft);
    Series_HRV->attachAxis(xAxis_HRV);
    Series_HRV->attachAxis(yAxis_HRV);
    return chart;
}

void HRV::setHRVData(QList<int>rr)
{

    Series_HRV->clear();
    int rr_Len=rr.size();
    xAxis_HRV->setRange(0, rr_Len);
    for(int i=0;i<rr_Len;i++)
    {
        Series_HRV->append(QPointF(i,rr.at(i)));
    }
}
void HRV::InitialAxis_Chart_AR()
{
    yAxis_AR->setRange(0, 1200);
    xAxis_AR->setRange(0, 128);
    xAxis_AR->setLabelsFont( QFont("微软雅黑", 12, QFont::Normal, false));
    yAxis_AR->setLabelsFont( QFont("微软雅黑", 12, QFont::Normal, false));
    yAxis_AR->setTitleText("HRV曲线图");
    yAxis_AR->setTitleBrush(QBrush(QColor(0x2E,0x5A,0x82),Qt::SolidPattern));
    yAxis_AR->setLabelsColor(QColor(0x2E,0x5A,0x82));
    yAxis_AR->setGridLineVisible(false);
    yAxis_AR->setGridLineVisible(true);
    yAxis_AR->setGridLineColor(QColor(0xC4,0x91,0x44,0x20));
    xAxis_AR->setLineVisible(false);
    xAxis_AR->setVisible(false);
    yAxis_AR->setTitleText("msec");
    yAxis_AR->setVisible(true);
}
QChart* HRV::CreateLineChart_AR()
{
    QChart *chart = new QChart();
    Series_AR->setPen(QPen(QColor(0x00,0x00,0x00),2));//设置波形颜色
    chart->addSeries(Series_AR);
    chart->setBackgroundVisible(true);
    chart->legend()->hide(); /* hide legend */
    chart->addAxis(xAxis_AR, Qt::AlignBottom);
    chart->addAxis(yAxis_AR, Qt::AlignLeft);
    Series_AR->attachAxis(xAxis_AR);
    Series_AR->attachAxis(yAxis_AR);
    return chart;
}

void HRV::setARData(QList<double>freq,QList<double>psd)
{

    Series_AR->clear();
    double max=psd.at(0);
    double min=psd.at(0);
    qDebug()<<psd<<"size="<<psd.size();
    for(int i=0;i<128;i++)
    {
        if(max<psd.at(i))
            max=psd.at(i);
        if(min<psd.at(i))
            min=psd.at(i);
        Series_AR->append(QPointF(freq.at(i),psd.at(i)));
    }
    yAxis_AR->setRange(0, max);
    xAxis_AR->setRange(0,freq.last());
}
